package cyou.breathe.blog.user.facade;

import cyou.breathe.blog.api.user.request.UserQueryRequest;
import cyou.breathe.blog.api.user.request.UserRegisterRequest;
import cyou.breathe.blog.api.user.request.condition.*;
import cyou.breathe.blog.api.user.response.UserOperatorResponse;
import cyou.breathe.blog.api.user.response.UserQueryResponse;
import cyou.breathe.blog.api.user.response.data.UserInfo;
import cyou.breathe.blog.api.user.service.UserFacadeService;
import cyou.breathe.blog.user.domain.entity.User;
import cyou.breathe.blog.user.domain.entity.convertor.UserConvertor;
import cyou.breathe.blog.user.domain.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author: breathe
 * @createTime: 2025-08-27
 */
@DubboService(version = "1.0.0")
public class UserFacadeServiceImpl implements UserFacadeService {

    @Autowired
    private UserService userService;

    @Override
    public UserOperatorResponse register(UserRegisterRequest userRegisterRequest) {
        return userService.register(userRegisterRequest.getNumber(), userRegisterRequest.getInviteCode(), userRegisterRequest.getPassword());
    }

    @Override
    public UserQueryResponse<UserInfo> query(UserQueryRequest userQueryRequest) {

        //通过查询的情况，来根据不同的信息获取不同的结果， 这里也是一个比较优化的写法
        User user = switch (userQueryRequest.getUserQueryCondition()) {
            case UserIdQueryCondition userIdQueryCondition:
                yield userService.findById(userIdQueryCondition.getUserId());
            case UserPhoneQueryCondition userPhoneQueryCondition:
                yield userService.findByTelephone(userPhoneQueryCondition.getTelephone());
            case UserPhoneAndPasswordQueryCondition userPhoneAndPasswordQueryCondition:
                yield userService.findByPhoneAndPassword(userPhoneAndPasswordQueryCondition.getTelephone(), userPhoneAndPasswordQueryCondition.getPassword());
            case UserEmailQueryCondition userEmailQueryCondition:
                yield userService.findByEmail(userEmailQueryCondition.getEmail());
            case UserEmailAndPasswordQueryCondition userEmailAndPasswordQueryCondition:
                yield userService.findByEmailAndPassword(userEmailAndPasswordQueryCondition.getEmail(), userEmailAndPasswordQueryCondition.getPassword());
            default:
                throw new UnsupportedOperationException(userQueryRequest.getUserQueryCondition() + "'' 不支持");
        };

        UserQueryResponse<UserInfo> response = new UserQueryResponse();
        response.setSuccess(true);
        UserInfo userInfo = UserConvertor.INSTANCE.mapToVo(user);
        response.setData(userInfo);
        return response;
    }
}
